<template>
  <div>
    <div style="display: flex; justify-content: flex-start">
      <el-input
        placeholder="通过教师姓名搜索..."
        prefix-icon="el-icon-search"
        v-model="keywords"
        style="width: 400px"
        size="medium "
        clearable
      >
      </el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        size="mini"
        style="margin-left: 3px"
        @click="searchClick(keywords)"
        >搜索
      </el-button>
    </div>
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column property="name" label="姓名" width="120">
        <template slot-scope="scope"
          ><span style="color: #409eff; cursor: pointer">{{
            scope.row.name
          }}</span>
        </template>
      </el-table-column>
      <el-table-column property="avater" label="头像" width="120">
        <template slot-scope="scope"
          ><span style="color: #409eff; cursor: pointer">
            <img
              style="width: 80px; height: 80px"
              :src="imgsrcip + scope.row.avatar"
            />
          </span>
        </template>
      </el-table-column>
      <el-table-column property="academy" label="院系" width="120">
        <template slot-scope="scope"
          ><span style="color: #409eff; cursor: pointer">{{
            scope.row.academy
          }}</span>
        </template>
      </el-table-column>
      <el-table-column property="degree" label="学位" width="120">
        <template slot-scope="scope"
          ><span style="color: #409eff; cursor: pointer">{{
            scope.row.degree
          }}</span>
        </template>
      </el-table-column>
      <el-table-column property="email" label="邮箱" width="120">
        <template slot-scope="scope"
          ><span style="color: #409eff; cursor: pointer">{{
            scope.row.email
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage1"
      :page-sizes="[5, 10, 15]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog title="修改教师信息" :visible.sync="dialogVisible" width="30%">
      <el-form :model="teacher" label-width="100px">
        <el-form-item
          label="教师姓名"
          prop="name"
          placeholder="请输入教师姓名"
          width="200"
        >
          <el-input v-model="teacher.name"></el-input>
        </el-form-item>
        <el-form-item
          label="教师学位"
          prop="degree"
          placeholder="请输入教师学位"
          width="200"
        >
          <el-input v-model="teacher.degree"></el-input>
        </el-form-item>
        <el-form-item
          label="所属院系"
          prop="academy"
          placeholder="请输入教师所属院系"
          width="200"
        >
          <el-input v-model="teacher.academy"></el-input>
        </el-form-item>
        <el-form-item
          label="教师职称"
          prop="professional"
          placeholder="请输入教师职称"
          width="200"
        >
          <el-input v-model="teacher.professional"></el-input>
        </el-form-item>
        <el-form-item
          prop="email"
          label="教师邮箱"
          placeholder="请输入教师邮箱"
        >
          <el-input v-model="teacher.email"></el-input>
        </el-form-item>
        <el-form-item label="个人经历" prop="experience">
          <el-input type="textarea" v-model="teacher.experience"></el-input>
        </el-form-item>
        <el-form-item label="个人头像" prop="avatar">
          <!-- <img :src="'http://192.168.1.113:80'+teacher.avatar" alt="" style="width:90px;height:90px"> -->
          <el-upload
            class="avatar-uploader"
            :action="ITEMS_IMG_UPLOAD"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :show-file-list="false"
          >
            <img
              v-if="teacher.avatar"
              :src="imgsrcip + teacher.avatar"
              class="avatar"
              style="width: 90px; height: 90px"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="edittea('teacher')"
            >修改完成</el-button
          >
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  teacherlist,
  teadel,
  updatetea,
  teaid,
  ITEMS_IMG_UPLOAD,
  imgsrcip
} from "@/api/apis";
export default {
  data() {
    return {
      currentPage1: 1,
      keywords: "",
      tableData: [],
      loading: false,
      pageSize: 5,
      total: 1,
      dialogVisible: false,
      teacher: {},
      ITEMS_IMG_UPLOAD: "",
      imgsrcip:""
    };
  },
  created() {
    this.loading = true;
    this.ITEMS_IMG_UPLOAD = ITEMS_IMG_UPLOAD;
    this.imgsrcip=imgsrcip
    this.refresh();
  },
  methods: {
    handleEdit(row, index) {
      // console.log(row, index);
      // console.log(row.id);
      // console.log(index);
      this.dialogVisible = true;
      teaid(index.id).then((res) => {
        // console.log(res);
        this.teacher = { ...index };
        // console.log(this.teacher);
      });
    },
    edittea(teacher) {
      updatetea(this.teacher).then((res) => {
        // console.log(res);
        if (res.data.code == 200) {
          this.$message({
            type: "success",
            message: "修改成功!",
          });
          this.dialogVisible = false;
          this.refresh();
        }
      });
    },
    handleDelete(index, row) {
      // console.log(index.id);
      // console.log(row);
      this.$confirm("此操作将永久删除该用户数据, 是否继续?", "温馨提示", {
        confirmButtonText: "狠心删除",
        cancelButtonText: "取消操作",
        type: "warning",
      })
        .then(() => {
          teadel(row.id).then((res) => {
            // console.log(res);
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.refresh();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.refresh(this.pageSize);
    },
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.refresh(this.currentPage1);
    },
    refresh() {
      teacherlist(this.pageSize, this.currentPage1, this.keywords).then(
        (res) => {
          // console.log(res);
          this.tableData = res.data.data.records;
          // console.log(this.tableData)
          this.total = res.data.data.total;
          //   console.log(this.total);
          this.loading = false;
        }
      );
    },
    searchClick(keywords) {
      this.keywords = keywords;
      this.refresh(this.keywords);
    },
    handleAvatarSuccess(res) {
      // console.log(res);
      // console.log(res.code);
      // console.log(res.data);
      if (res.code == 200) {
        this.teacher.avatar = res.data;
      }
    },
  },
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader {
  border: 1px dashed #8c939d;
  width: 90px;
  height: 90px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 90px;
  height: 90px;
  line-height: 90px;
  text-align: center;
}
.avatar {
  width: 90px;
  height: 90px;
  display: block;
}
</style>